package com.engine4cloud.tdf.supervision.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.constant.enums.WatchTypeEnum;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.supervision.dto.create.JzdxTerminalManageCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.JzdxTerminalManageQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.EnumsTypeResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.JzdxTerminalManageResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.JzdxTerminalManageUpdateDTO;
import com.engine4cloud.tdf.supervision.service.JzdxTerminalManageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 矫正对象-监管设备绑定-web
 *
 * @author wenjie
 * @date 2024-04-19 11:21:59
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/cor/terminal/manage")
@Tag(description = "CorTerminalManage", name = "矫正对象-监管设备绑定管理-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class JzdxTerminalManageWebController {

    private final JzdxTerminalManageService terminalManageService;

    /**
     * 定位终端类型
     */
    @Operation(summary = "定位终端类型", description = "定位终端类型")
    @GetMapping("/all/types")
    public R<List<EnumsTypeResultDTO>> status() {
        List<EnumsTypeResultDTO> labelList = Arrays.stream(WatchTypeEnum.values())
                .map(state -> new EnumsTypeResultDTO(state.getType(), state.getDesc()))
                .collect(Collectors.toList());
        return R.ok(labelList);
    }

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param dto  矫正对象-监管设备绑定
     * @return R<PageInfo < JzdxInterdictResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<JzdxTerminalManageResultDTO>> getTerminalManagePage(@ParameterObject Params page, @Valid @ParameterObject JzdxTerminalManageQueryDTO dto) {
        return R.ok(terminalManageService.getTerminalManagePage(page, dto));
    }


    /**
     * 通过id查询矫正对象-监管设备绑定
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<JzdxTerminalManageResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(terminalManageService.getById(id));
    }

    /**
     * 查询设备
     *
     * @param dto dto
     * @return R
     */
    @Operation(summary = "查询设备", description = "查询设备")
    @GetMapping("/find")
    public R<JzdxTerminalManageResultDTO> findOne(@Valid @ParameterObject JzdxTerminalManageQueryDTO dto) {
        return R.ok(terminalManageService.findOne(dto));
    }

    /**
     * 新增矫正对象-监管设备绑定
     *
     * @param dto 矫正对象-监管设备绑定
     * @return R
     */
    @Operation(summary = "新增矫正对象-监管设备绑定", description = "新增矫正对象-监管设备绑定")
    @SysLog("新增矫正对象-监管设备绑定")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('corrector_terminal_manage_add')")
    public R<Boolean> create(@Valid @RequestBody JzdxTerminalManageCreateDTO dto) {
        return R.ok(terminalManageService.create(dto));
    }

    /**
     * 修改矫正对象-监管设备绑定
     *
     * @param dto 矫正对象-监管设备绑定
     * @return R
     */
    @Operation(summary = "修改矫正对象-监管设备绑定", description = "修改矫正对象-监管设备绑定")
    @SysLog("修改矫正对象-监管设备绑定")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('corrector_terminal_manage_edit')")
    public R<Boolean> update(@Valid @RequestBody JzdxTerminalManageUpdateDTO dto) {
        return R.ok(terminalManageService.update(dto));
    }

    /**
     * 通过id删除矫正对象-监管设备绑定
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除矫正对象-监管设备绑定", description = "通过id删除矫正对象-监管设备绑定")
    @SysLog("通过id删除矫正对象-监管设备绑定")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('corrector_terminal_manage_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(terminalManageService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 设备编号(包括手机号、手机唯一标识码、腕表imei号)是否唯一
     *
     * @param dto dto
     * @return 返回 true-唯一；false-不唯一
     */
    @Operation(summary = "设备编号唯一性校验", description = "设备编号唯一性校验")
    @PostMapping("/check")
    public R<Boolean> checkUnique(@Valid @RequestBody JzdxTerminalManageQueryDTO dto) {
        return R.ok(terminalManageService.checkUnique(dto.getId(), dto.getLocPhone(), dto.getPhoneIdentity(), dto.getWatchImei()));
    }
}